package com.liu.student.mapper;

import com.liu.student.entity.Teacher;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeacherMapper {
    @Select("SELECT * FROM teacher")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "teacherNumber", column = "teacher_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "department", column = "department"),
        @Result(property = "title", column = "title"),
        @Result(property = "hireDate", column = "hire_date")
    })
    List<Teacher> findAll();

    @Select("SELECT * FROM teacher WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "teacherNumber", column = "teacher_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "department", column = "department"),
        @Result(property = "title", column = "title"),
        @Result(property = "hireDate", column = "hire_date")
    })
    Teacher findById(Long id);

    @Select("SELECT t.* FROM teacher t " +
            "INNER JOIN user u ON t.user_id = u.id " +
            "WHERE u.username = #{username}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "teacherNumber", column = "teacher_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "department", column = "department"),
        @Result(property = "title", column = "title"),
        @Result(property = "hireDate", column = "hire_date")
    })
    Teacher findByUsername(String username);

    @Insert("INSERT INTO teacher (user_id, teacher_number, name, age, gender, department, title, hire_date) " +
            "VALUES (#{userId}, #{teacherNumber}, #{name}, #{age}, #{gender}, #{department}, #{title}, #{hireDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Teacher teacher);

    @Update("UPDATE teacher SET name = #{name}, age = #{age}, gender = #{gender}, " +
            "department = #{department}, title = #{title} WHERE id = #{id}")
    int update(Teacher teacher);

    @Delete("DELETE FROM teacher WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT COUNT(*) FROM teacher")
    long count();
}